home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 October / Macworld (1998-10).dmg / Shareware World / Info / For Developers / MacZoop 1.8.4 / More Classes / Window Classes / ZPictWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-05  |  8.2 KB  |  382 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZPictWindow.cpp        -- a window that displays PICT files
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZPictWindow.h"
  23. #include    "MacZoop.h"
  24. #include    "ZFile.h"
  25.  
  26. #include    <ImageCompression.h>
  27.  
  28. CLASSCONSTRUCTOR( ZPictWindow );
  29.  
  30. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  31.  
  32. ZPictWindow::ZPictWindow( ZCommander* aBoss, const short windID )
  33.     : ZScroller( aBoss, windID, TRUE, TRUE )
  34. {
  35.     classID = CLASS_ZPictWindow;
  36.     
  37.     thePicture = NULL;
  38.     printable = TRUE;
  39.     
  40.     savePreview = FALSE;
  41.     saveCustomIcon = FALSE;
  42. }
  43.  
  44.  
  45. ZPictWindow::ZPictWindow()
  46.     : ZScroller()
  47. {
  48.     classID = CLASS_ZPictWindow;
  49.     
  50.     thePicture = NULL;
  51.     printable = TRUE;
  52.     
  53.     savePreview = FALSE;
  54.     saveCustomIcon = FALSE;
  55. }
  56.  
  57.  
  58. /*---------------------------------***  DESTRUCTOR  ***---------------------------------*/
  59.  
  60. ZPictWindow::~ZPictWindow()
  61. {
  62.     if (thePicture)
  63.         KillPicture(thePicture);
  64. }
  65.  
  66. /*--------------------------------***  DRAWCONTENT  ***---------------------------------*/
  67. /*    
  68.  
  69. overrides ZScroller to draw the picture in the window
  70.  
  71. ----------------------------------------------------------------------------------------*/
  72.  
  73. void    ZPictWindow::DrawContent()
  74. {
  75.     Rect    pFrame;
  76.     
  77.     if (thePicture)
  78.     {
  79.         pFrame = (*thePicture)->picFrame;
  80.         OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  81.         DrawPicture(thePicture, &pFrame);
  82.     }
  83. }
  84.  
  85.  
  86. /*----------------------------------***  OPENFILE  ***----------------------------------*/
  87. /*    
  88.  
  89. overrides the base ZWindow to open a PICT file on disk to the window
  90.  
  91. ----------------------------------------------------------------------------------------*/
  92.  
  93. void    ZPictWindow::OpenFile( const OSType fType, Boolean isStationery )
  94. {
  95.     // read the picture into the picture handle
  96.     
  97.     FInfo    fi;
  98.     short    refNum;
  99.     long    pSize;
  100.     Handle    temp = NULL;
  101.     Rect    pFrame;
  102.     
  103.     if (macFile.vRefNum != kNoFile)
  104.     {
  105.         FailOSErr(FSpGetFInfo(&macFile, &fi));    
  106.         
  107.         if (fi.fdType != 'PICT')
  108.             FailOSErr(paramErr);
  109.             
  110.         FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
  111.         
  112.         // skip the first 512 bytes of the file
  113.         
  114.         try
  115.         {
  116.             FailOSErr(SetFPos(refNum, fsFromStart,  512));
  117.             
  118.             // how big is the rest of the file?
  119.             
  120.             FailOSErr(GetEOF(refNum, &pSize));
  121.             pSize -= 512;
  122.             
  123.             if (pSize > 0)
  124.             {
  125.                 FailNIL(temp = NewHandle(pSize));
  126.             
  127.                 HLock(temp);
  128.                 FailOSErr(FSRead(refNum, &pSize, *temp));
  129.                 HUnlock(temp);
  130.             }
  131.             
  132.             FSClose(refNum);
  133.             
  134.             if (thePicture)
  135.                 KillPicture(thePicture);
  136.                 
  137.             thePicture  = (PicHandle)temp;
  138.             
  139.             // set the scroll dimensions to the picture's frame
  140.             
  141.             pFrame = (*thePicture)->picFrame;
  142.             OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  143.             SetBounds( pFrame );
  144.             SetScrollAmount(8,8);
  145.             
  146.             pFrame.right += kStdScrollbarWidth;
  147.             pFrame.bottom += kStdScrollbarWidth;
  148.             
  149.             SetSize(pFrame.right, pFrame.bottom);
  150.         }
  151.         catch(OSErr err)
  152.         {
  153.             FSClose(refNum);
  154.             
  155.             if (temp)
  156.             {
  157.                 HUnlock(temp);
  158.                 DisposeHandle(temp);
  159.             }    
  160.             throw err;
  161.         }
  162.     }
  163.     ZScroller::OpenFile( fType, isStationery );
  164. }
  165.  
  166.  
  167.  
  168. /*--------------------------------***  SAVEFILE  ***---------------------------------*/
  169. /*    
  170.  
  171. overrides the base ZWindow to save the image as a PICT file on disk
  172.  
  173. ----------------------------------------------------------------------------------------*/
  174.  
  175. void    ZPictWindow::SaveFile()
  176. {
  177.     // this saves the window contents as a PICT file in the file <macFile>.
  178.  
  179.     Handle        header = NULL;
  180.     ZFile*        theFile;
  181.     
  182.     if ((macFile.vRefNum != kNoFile) && (thePicture != NULL))
  183.     {
  184.         FailNIL( theFile = new ZFile( macFile ));
  185.         theFile->SetType( 'PICT' );
  186.         
  187.         if (! theFile->IsReal())
  188.             theFile->Create();
  189.         
  190.         // open the file for safe-saving
  191.             
  192.         theFile->OpenSafe();
  193.         
  194.         try
  195.         {
  196.             // make a header and write it out. This is just 512 bytes of zeroes.
  197.             
  198.             FailNIL( header = NewHandleClear( 512L ));
  199.             
  200.             theFile->Write( header );
  201.             DisposeHandle(header);
  202.             header = NULL;
  203.             
  204.             // now write the picture handle
  205.             
  206.             theFile->Write((Handle) thePicture );
  207.             
  208.             // if the image comp mgr and QTime is available, make a preview and save it
  209.             
  210.             if ( gMacInfo.hasImgCompressionMgr &&
  211.                  gMacInfo.hasQuickTime &&
  212.                  savePreview )
  213.             {
  214.                 PicHandle    preview;
  215.                 short        ref;
  216.                 
  217.                 FailNIL( preview = (PicHandle) NewHandle( 0 ));
  218.                 FailOSErr( MakeThumbnailFromPicture( thePicture, 8, preview, NULL ));    
  219.                 
  220.                 theFile->CreateResFork();
  221.                 theFile->OpenResFork();
  222.                 
  223.                 ref = theFile->GetResourceRefNumber();
  224.                 
  225.                 try
  226.                 {
  227.                     FailOSErr( AddFilePreview( ref,'PICT', (Handle) preview ));
  228.                 }
  229.                 catch( OSErr err )
  230.                 {
  231.                     theFile->CloseResFork();
  232.                     throw err;
  233.                 }
  234.                 
  235.                 theFile->CloseResFork();
  236.             }
  237.             
  238.             if ( saveCustomIcon )
  239.                 theFile->MakeCustomIcon( thePicture );
  240.     
  241.             theFile->Close();
  242.             macFType = 'PICT';
  243.         }
  244.         catch( OSErr err )
  245.         {    
  246.             theFile->Close();
  247.             ForgetObject( theFile );
  248.             
  249.             if (header)
  250.                 DisposeHandle(header);
  251.             
  252.             throw err;
  253.         }
  254.         
  255.         ForgetObject( theFile );
  256.         ZScroller::SaveFile();
  257.     }
  258. }
  259.  
  260. /*--------------------------------***  UPDATEMENUS  ***---------------------------------*/
  261. /*
  262. Enable the Copy command if we have a picture    
  263. ----------------------------------------------------------------------------------------*/
  264.  
  265. void    ZPictWindow::UpdateMenus()
  266. {
  267.     if ( thePicture )
  268.         gMenuBar->EnableCommand( kCmdCopy );
  269.  
  270.     ZScroller::UpdateMenus();
  271. }
  272.  
  273.  
  274. /*--------------------------------***  CANPASTETYPE  ***--------------------------------*/
  275. /*    
  276. we can accept Paste commands of type PICT
  277. ----------------------------------------------------------------------------------------*/
  278.  
  279. Boolean    ZPictWindow::CanPasteType()
  280. {
  281.     return gClipboard->QueryType( 'PICT' );
  282. }
  283.  
  284.  
  285. /*-----------------------------------***  DOCOPY  ***-----------------------------------*/
  286. /*
  287. copy the image to the clipboard    
  288. ----------------------------------------------------------------------------------------*/
  289.  
  290. void    ZPictWindow::DoCopy()
  291. {
  292.     if ( thePicture )
  293.         gClipboard->PutData( thePicture );
  294. }
  295.  
  296. /*----------------------------------***  DOPASTE  ***-----------------------------------*/
  297. /*
  298. copy a PICT from the clipboard to our local picture    
  299. ----------------------------------------------------------------------------------------*/
  300.  
  301. void    ZPictWindow::DoPaste()
  302. {
  303.     Rect        pFrame;
  304.     PicHandle    p = (PicHandle) gClipboard->GetData( 'PICT' );
  305.     
  306.     FailNIL( p );
  307.     
  308.     if ( thePicture )
  309.         KillPicture( thePicture );
  310.         
  311.     thePicture = p;
  312.     
  313.     pFrame = (*thePicture)->picFrame;
  314.     OffsetRect( &pFrame, -pFrame.left, -pFrame.top );
  315.     SetBounds( pFrame );
  316.     SetScrollAmount( 8, 8 );
  317.     
  318.     pFrame.right += kStdScrollbarWidth;
  319.     pFrame.bottom += kStdScrollbarWidth;
  320.     
  321.     SetSize( pFrame.right, pFrame.bottom );
  322.     
  323.     dirty = TRUE;
  324. }
  325.  
  326.  
  327. /*--------------------------***  SETPICTUREFROMRESOURCE  ***----------------------------*/
  328. /*
  329. set the picture from a current resource file
  330. ----------------------------------------------------------------------------------------*/
  331.  
  332. void    ZPictWindow::SetPictureFromResource( short pictResID )
  333. {
  334.     PicHandle    p = GetPicture( pictResID );
  335.     
  336.     FailNILRes( p );
  337.     
  338.     KillPicture( thePicture );
  339.     thePicture = p;
  340.     
  341.     Rect    pFrame = (*thePicture)->picFrame;
  342.     OffsetRect( &pFrame, -pFrame.left, -pFrame.top );
  343.     SetBounds( pFrame );
  344.     
  345.     dirty = TRUE;
  346. }
  347.  
  348.  
  349. /*-------------------------------***  WRITETOSTREAM  ***--------------------------------*/
  350. /*
  351. write window state and picture itself to the stream    
  352. ----------------------------------------------------------------------------------------*/
  353.  
  354. void    ZPictWindow::WriteToStream( ZStream* aStream )
  355. {
  356. #if _MACZOOP_STREAMS
  357.     ZScroller::WriteToStream( aStream );
  358.     
  359.     aStream->WriteChar( savePreview );
  360.     aStream->WriteChar( saveCustomIcon );
  361.     aStream->WriteHandle((Handle) thePicture );
  362. #endif
  363. }
  364.  
  365.  
  366. /*------------------------------***  READFROMSTREAM  ***--------------------------------*/
  367. /*
  368. read window state and picture itself from the stream    
  369. ----------------------------------------------------------------------------------------*/
  370.  
  371. void    ZPictWindow::ReadFromStream( ZStream* aStream )
  372. {
  373. #if _MACZOOP_STREAMS
  374.     ZScroller::ReadFromStream( aStream );
  375.     
  376.     aStream->ReadChar((char*) &savePreview );
  377.     aStream->ReadChar((char*) &saveCustomIcon );
  378.     aStream->ReadHandle((Handle*) &thePicture );
  379. #endif
  380. }
  381.  
  382.